home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / mail / pine3.96.tar.gz / pine3.96.tar / pine3.96 / imap / non-ANSI / c-client / strtoul.c < prev    next >
C/C++ Source or Header  |  1995-03-23  |  4KB  |  114 lines

  1. /*
  2.  * Program:    String to unsigned long
  3.  *
  4.  * Author:    Mark Crispin
  5.  *        Networks and Distributed Computing
  6.  *        Computing & Communications
  7.  *        University of Washington
  8.  *        Administration Building, AG-44
  9.  *        Seattle, WA  98195
  10.  *
  11.  * Date:    14 February 1995
  12.  * Last Edited:    23 March 1995
  13.  *
  14.  * Copyright 1995 by the University of Washington
  15.  *
  16.  *  Permission to use, copy, modify, and distribute this software and its
  17.  * documentation for any purpose and without fee is hereby granted, provided
  18.  * that the above copyright notice appears in all copies and that both the
  19.  * above copyright notice and this permission notice appear in supporting
  20.  * documentation, and that the name of the University of Washington not be
  21.  * used in advertising or publicity pertaining to distribution of the software
  22.  * without specific, written prior permission.  This software is made
  23.  * available "as is", and
  24.  * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
  25.  * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED
  26.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
  27.  * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
  28.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  29.  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
  30.  * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
  31.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  32.  *
  33.  */
  34.  
  35. /*
  36.  * Turn a string unsigned long into the real thing
  37.  * Accepts: source string
  38.  *        pointer to place to return end pointer
  39.  *        base
  40.  * Returns: parsed unsigned long integer, end pointer is updated
  41.  */
  42.  
  43. unsigned long strtoul (s,endp,base)
  44.     char *s;
  45.     char **endp;
  46.     int base;
  47. {
  48.   unsigned long value = 0;    /* the accumulated value */
  49.   /* Yes, you can feed it negative number strings */
  50.   int negative = 0;        /* this a negative number? */
  51.   int ok;            /* true while valid chars for number */
  52.   char c;
  53.   if (base && (base < 2 || base > 36)) {
  54.     errno = EINVAL;        /* insist upon valid base */
  55.     return NIL;
  56.   }
  57.   while (isspace (*s)) s++;    /* skip leading whitespace */
  58.   if (base) {            /* if have a base */
  59.     switch (*s) {        /* check for leading sign char */
  60.     case '-':
  61.       negative = 1;        /* yes, negative #.  fall into '+' */
  62.     case '+':
  63.       s++;            /* skip the sign character */
  64.     }
  65.                 /* allow hex prefix "0x" */
  66.     if (base == 16 && *s == '0' && toupper (*(s + 1)) == 'X') s += 2;
  67.     do {            /* convert to numeric form if digit*/
  68.       if (isdigit (*s)) c = toint (*s);
  69.                 /* alphabetic conversion */
  70.       else if (isalpha (*s)) c = *s - (isupper (*s) ? 'A' : 'a') + 10;
  71.       else break;        /* else no way it's valid */
  72.       if (c >= base) break;    /* digit out of range for base? */
  73.       value = value * base + c;    /* accumulate the digit */
  74.     } while (*++s);        /* loop until non-numeric character */
  75.   }
  76.  
  77.   else {            /* if base = 0, */
  78.     if (*s == '-') {        /* integer constants are allowed a */
  79.       negative = 1;        /* leading unary minus, but not */
  80.       s++;            /* unary plus. */
  81.     }
  82.     if (*s == '0') {        /* if it starts with 0, its either */
  83.       if (toupper (*++s)=='X') {/* 0X, which marks a hex constant, */
  84.     s++;            /* skip the X */
  85.            base = 16;        /* base is hex 16 */
  86.       }
  87.       else base = 8;        /* leading 0 means octal number */
  88.     }
  89.     else base = 10;        /* otherwise, decimal. */
  90.     do {
  91.       switch (base) {        /* char has to be valid for base */
  92.       case 8:            /* this digit ok? */
  93.     ok = isodigit (*s);
  94.     break;
  95.       case 10:
  96.     ok = isdigit (*s);
  97.     break;
  98.       case 16:
  99.     ok = isxdigit (*s);
  100.     break;
  101.       default:            /* it's good form you know */
  102.     return NIL;
  103.       }
  104.                 /* if valid char, accumulate */
  105.       if (ok) value = value * base + toint (*s++);
  106.     } while (ok);
  107.     if (toupper (*s) == 'L')s++;/* ignore 'l' or 'L' marker */
  108.   }
  109.  
  110.   if (endp) *endp = s;        /* save users endp to after number */
  111.                 /* negate number if needed */
  112.   return (negative) ? -value : value;
  113. }
  114.